package swu.edu.cn.javaee.controller;


import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.Product;
import swu.edu.cn.javaee.entity.ProductInfo;
import swu.edu.cn.javaee.service.ProductService;

@Controller
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 添加商品
     * @param product
     * @return
     */
    @RequestMapping("/saveProduct")
    @ResponseBody
    public String saveProduct(Product product){
        String result= productService.saveProduct(product);
        return result;
    }

    /**
     * 查询用户的商品信息
     * @param pUid
     * @return
     */
    @RequestMapping("/queryProduct")
    @ResponseBody
    public ProductInfo queryProduct(String pUid){
        ProductInfo productInfo=new ProductInfo();
        productInfo=productService.queryProduct(pUid);
        return productInfo;
    }

    /**
     * 展示推荐至前五的产品数据
     * @return
     */
    @RequestMapping("/queryByRecommend")
    @ResponseBody
    public ProductInfo queryByRecommend(String pUid){
        ProductInfo productInfo=new ProductInfo();
        productInfo=productService.queryFProduct(pUid);
        System.out.println("ProdutcController\n"+productInfo);
        return productInfo;
    }

    /**
     * 更新产品信息
     * @param product
     * @return
     */
    @RequestMapping("/updateProduct")
    @ResponseBody
    public String updateProduct(Product product){
        String result=productService.updateProduct(product);
        return result;
    }


    /**商品推荐值更新,id==1,增
     * */
    @RequestMapping("/updateRecommend")
    @ResponseBody
    public String updateRecommend(Product product,String id){
        String new_recommend;
        String core=product.getpRecommend();
        if(id.equals("1")){
            new_recommend=String.valueOf(Integer.valueOf(core)+1);
        }
        else {
            new_recommend=String.valueOf(Integer.valueOf(core)-1);
        }
        product.setpRecommend(new_recommend);
        String result=productService.updateProduct(product);
        return result;
    }
    /**
     * 删除商品信息
     * @param pid
     * @return
     */
    @RequestMapping("/deleteProduct")
    @ResponseBody
    public String deleteProduct(String pid){
        String result=productService.deleteProduct(pid);
        return result;
    }
}
